/**
 * 签署位API接口
 */
import axios from 'axios'

// 创建axios实例
const request = axios.create({
  baseURL: '/api',
  timeout: 30000
})

// 响应拦截器
request.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// 模拟数据
const mockData = {
  // 文件列表模拟数据 - 使用本地HTML文件作为PDF预览
  fileList: [
    {
      id: 'file_001',
      fileId: 'file_001',
      name: '合同协议书.pdf',
      url: '/sample-pdfs/contract.html', // 本地HTML文件路径
      signStatus: 0
    },
    {
      id: 'file_002',
      fileId: 'file_002',
      name: '委托授权书.pdf',
      url: '/sample-pdfs/authorization.html', // 本地HTML文件路径
      signStatus: 0
    }
  ],
  
  // 签署主体模拟数据
  signSubjects: [
    {
      id: 'subject_001',
      name: '张三',
      identityInfo: [
        { identityId: 'identity_001', identityName: '法定代表人' }
      ]
    },
    {
      id: 'subject_002',
      name: '李四',
      identityInfo: [
        { identityId: 'identity_002', identityName: '经办人' }
      ]
    },
    {
      id: 'subject_003',
      name: '王五',
      identityInfo: [
        { identityId: 'identity_003', identityName: '财务负责人' }
      ]
    }
  ],
  
  // 签章位置模拟数据
  signPositions: {
    'file_001': {
      siteInfos: [
        {
          siteId: 'site_001',
          page: 1,
          posx: 100,
          posy: 200,
          width: 80,
          height: 80,
          signMethod: 1, // 公章
          signSubjectId: 'subject_001'
        }
      ],
      pagingSealInfos: []
    },
    'file_002': {
      siteInfos: [],
      pagingSealInfos: []
    }
  },
  
  // 关键字位置模拟数据
  keywordPositions: {
    '甲方': [
      { page: 1, x: 150, y: 300 },
      { page: 2, x: 200, y: 400 }
    ],
    '乙方': [
      { page: 1, x: 350, y: 300 },
      { page: 2, x: 400, y: 400 }
    ],
    '签字': [
      { page: 3, x: 250, y: 500 }
    ]
  }
}

// 模拟延迟函数
const mockDelay = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms))

// 模拟响应格式
const mockResponse = (data, code = 0, message = 'success') => ({
  code,
  data,
  message
})

// 签署位API
export default {
  /**
   * 批量保存签章位置
   * @param {Object} data 签章位置数据
   */
  async saveSiteInfo(data) {
    await mockDelay(300)
    console.log('保存签章位置:', data)
    // 更新模拟数据
    if (data.signSiteInfos && data.signSiteInfos.length > 0) {
      const fileInfo = data.signSiteInfos[0]
      if (fileInfo.agreementId && mockData.signPositions[fileInfo.agreementId]) {
        mockData.signPositions[fileInfo.agreementId] = {
          siteInfos: fileInfo.siteInfos || [],
          pagingSealInfos: fileInfo.pagingSealInfos || []
        }
      }
    }
    return mockResponse({ success: true })
  },

  /**
   * 保存单个签章位置
   * @param {Object} data 签章位置数据
   */
  async singleSaveSiteInfo(data) {
    await mockDelay(200)
    console.log('保存单个签章位置:', data)
    // 更新模拟数据
    if (data.signSiteInfos && data.signSiteInfos.length > 0) {
      const fileInfo = data.signSiteInfos[0]
      if (fileInfo.agreementId && mockData.signPositions[fileInfo.agreementId]) {
        mockData.signPositions[fileInfo.agreementId] = {
          siteInfos: fileInfo.siteInfos || [],
          pagingSealInfos: fileInfo.pagingSealInfos || []
        }
      }
    }
    return mockResponse({ success: true })
  },

  /**
   * 保存草稿
   * @param {Object} data 签章位置数据
   */
  async draftSiteInfo(data) {
    await mockDelay(200)
    console.log('保存草稿:', data)
    return mockResponse({ success: true })
  },

  /**
   * 查询签章位置
   * @param {Object} params 查询参数
   */
  async querySiteInfo(params) {
    await mockDelay(300)
    console.log('查询签章位置:', params)
    const fileId = params.fileId || params.agreementId
    const positions = mockData.signPositions[fileId] || { siteInfos: [], pagingSealInfos: [] }
    return mockResponse({
      signSiteInfos: [{
        agreementId: fileId,
        ...positions
      }]
    })
  },

  /**
   * 获取关键字位置
   * @param {Object} params 关键字参数
   */
  async getKeywordPosition(params) {
    await mockDelay(400)
    console.log('获取关键字位置:', params)
    const keyword = params.keyWord || params.keyword
    const positions = mockData.keywordPositions[keyword] || []
    return mockResponse({
      positions: positions.map(pos => ({
        page: pos.page,
        x: pos.x,
        y: pos.y
      }))
    })
  },

  /**
   * 删除签章
   * @param {Object} data 签章ID
   */
  async deleteSign(data) {
    await mockDelay(200)
    console.log('删除签章:', data)
    // 从模拟数据中删除指定签章
    const { fileId, siteId } = data
    if (fileId && siteId && mockData.signPositions[fileId]) {
      const positions = mockData.signPositions[fileId]
      positions.siteInfos = positions.siteInfos.filter(site => site.siteId !== siteId)
    }
    return mockResponse({ success: true })
  },

  /**
   * 删除所有签章
   * @param {Object} data 文件ID
   */
  async deleteAll(data) {
    await mockDelay(200)
    console.log('删除所有签章:', data)
    // 清空指定文件的所有签章
    const { fileId } = data
    if (fileId && mockData.signPositions[fileId]) {
      mockData.signPositions[fileId] = {
        siteInfos: [],
        pagingSealInfos: []
      }
    }
    return mockResponse({ success: true })
  },

  /**
   * 获取文件列表
   * @param {Object} params 查询参数
   */
  async getFileList(params) {
    await mockDelay(300)
    console.log('获取文件列表:', params)
    return mockResponse({
      fileList: mockData.fileList
    })
  },

  /**
   * 获取签署主体列表
   * @param {Object} params 查询参数
   */
  async getSignSubjects(params) {
    await mockDelay(200)
    console.log('获取签署主体列表:', params)
    return mockResponse({
      signSubjects: mockData.signSubjects
    })
  }
}